package com.ypj.indusynergyems.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.j2objc.annotations.Weak;
import com.ypj.indusynergyems.dao.entity.CartEntity;
import com.ypj.indusynergyems.dao.entity.OrderEntity;
import com.ypj.indusynergyems.dao.mapper.CartMapper;
import com.ypj.indusynergyems.dao.mapper.OrderMapper;
import com.ypj.indusynergyems.service.IOrderService;
import lombok.RequiredArgsConstructor;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@RequiredArgsConstructor
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, OrderEntity> implements IOrderService {

    private final CartMapper cartMapper;

    @Override
    public String addCart(int goodsId, int num, double price, int userId,int shopId) {
        try {
            LambdaQueryWrapper<CartEntity> queryWrapper = Wrappers.lambdaQuery(CartEntity.class)
                    .eq(CartEntity::getUserId, userId)
                    .eq(CartEntity::getGoodsId, goodsId);
            CartEntity cartEntity = cartMapper.selectOne(queryWrapper);

            if (cartEntity != null) {
                log.info("Cart record found for user {} and goods {}", userId, goodsId);
                cartEntity.setCount(cartEntity.getCount() + num);
                LambdaUpdateWrapper<CartEntity> updateWrapper = Wrappers.lambdaUpdate(CartEntity.class)
                        .eq(CartEntity::getUserId, userId)
                        .eq(CartEntity::getGoodsId, goodsId);
                return executeDatabaseOperation(() -> cartMapper.updateById(cartEntity));
            } else {
                log.info("No cart record found for user {} and goods {}, creating new record", userId, goodsId);
                CartEntity cart = CartEntity.builder()
                        .goodsId(goodsId)
                        .count(num)
                        .price(price)
                        .shopId(shopId)
                        .userId(userId)
                        .build();
                return executeDatabaseOperation(() -> cartMapper.insert(cart));
            }
        } catch (Exception e) {
            log.error("Error occurred while adding or updating cart: ", e);
            return "fail";
        }
    }

    @Override
    public List<CartEntity> findMyCartList(int userId) {
        return cartMapper.findMyCartList(userId);
    }

    @Override
    public List<CartEntity> findMySelectGoodsList(int[] ints) {
        return cartMapper.findMySelectGoodsList(ints);
    }

    private String executeDatabaseOperation(Runnable operation) {
        try {
            operation.run();
            return "success";
        } catch (Exception e) {
            log.error("Database operation failed", e);
            return "fail";
        }
    }
}
